﻿using AntMovement.Core;
using AntMovement.Core.Entity;
using AntMovement.Core.Enum;
using AntMovement.Core.IParameter;
using AntMovement.Core.Tool;
using AntMovement.Menu.Device.ViewModels.Dialog;
using AntMovement.Menu.Device.Views;
using AntMovement.Menu.Device.Views.Dialog;
using AntMovement.Parameter;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.DirectoryServices.ActiveDirectory;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace AntMovement.Menu.Device.ViewModels
{
    public class IOConfigViewModel : Prism.Mvvm.BindableBase
    {
        private ObservableCollection<DI> _DIDataSet;

        public ObservableCollection<DI> DIDataSet
        {
            get { return _DIDataSet; }
            set { SetProperty(ref _DIDataSet, value); }
        }

        private ObservableCollection<DO> _DODataSet;

        public ObservableCollection<DO> DODataSet
        {
            get { return _DODataSet; }
            set { SetProperty(ref _DODataSet, value); }
        }


        public IOParameter IOP { get; set; }
        private IDialogHost _dialogHost;
        public IOConfigViewModel(IIOParameter iOParameter, IDialogHost dialogHost)
        {
            IOP = (IOParameter)iOParameter;
            DIDataSet = new ObservableCollection<DI>(IOP.DIDataSet);
            DODataSet = new ObservableCollection<DO>(IOP.DODataSet);
            _dialogHost = dialogHost;
        }

        public DelegateCommand Add => new DelegateCommand(async () =>
        {
            object rest = await DialogHost.Show(new IOAdd() { DataContext = new IOAddDialogModel(IOP) }, PopUpType.Global.ToString());
            DIDataSet = new ObservableCollection<DI>(IOP.DIDataSet);
            DODataSet = new ObservableCollection<DO>(IOP.DODataSet);
            if (rest == null)
            {
                if (IOP.Save())
                {
                    _ = _dialogHost.DialogShow("保存成功", LogLevel.Success, isOperation: true, PopUpType.Global);
                }
                else
                {
                    _ = _dialogHost.DialogShow("保存失败", LogLevel.Error, isOperation: true, PopUpType.Global);
                }
            }
        });

        public DelegateCommand Save => new DelegateCommand(() =>
        {
            if (IOP.Save())
            {
                _ = _dialogHost.DialogShow("保存成功", LogLevel.Success, isOperation: true, PopUpType.Global);
            }
            else
            {
                _ = _dialogHost.DialogShow("保存失败", LogLevel.Error, isOperation: true, PopUpType.Global);
            }
        });

        public DelegateCommand Delete => new DelegateCommand(() =>
        {
            IOP.DODataSet.Clear();
            IOP.DIDataSet.Clear();
            DIDataSet.Clear();
            DODataSet.Clear();
            DIDataSet = new ObservableCollection<DI>(IOP.DIDataSet);
            DODataSet = new ObservableCollection<DO>(IOP.DODataSet);
            IOP.Save();
        });

        private Action<DI, DI> _copyDI;
        public DelegateCommand<DataGrid> InputSelectDelete => new DelegateCommand<DataGrid>(sender =>
        {
            if (_copyDI == null)
            {
                _copyDI = ConversionHelper.CopyClass<DI,DI>();
            }
            if (sender.SelectedItems == null || sender.SelectedItems.Count == 0)
            {
                return;
            }
            List<DI> DIList = new List<DI>();
            foreach (DI item in sender.SelectedItems)
            {
                DI model = new DI();
                _copyDI.Invoke(item, model);
                DIList.Add(model);
            }
            foreach (DI item in DIList) 
            {
                var model = IOP.DIDataSet.FirstOrDefault(x => x.Index == item.Index);
                if (model != null) 
                {
                    IOP.DIDataSet.Remove(model);
                }
            }
            for (int i = 0; i < IOP.DIDataSet.Count; i++) 
            {
                IOP.DIDataSet[i].Index = i + 1;
            }
            DIDataSet.Clear();
            DIDataSet = new ObservableCollection<DI>(IOP.DIDataSet);
            IOP.Save();
        });

        private Action<DO, DO> _copyDO;
        public DelegateCommand<DataGrid> OutSelectDelete => new DelegateCommand<DataGrid>(sender =>
        {
            if (_copyDO == null)
            {
                _copyDO = ConversionHelper.CopyClass<DO, DO>();
            }
            if (sender.SelectedItems == null || sender.SelectedItems.Count == 0)
            {
                return;
            }
            List<DO> DIList = new List<DO>();
            foreach (DO item in sender.SelectedItems)
            {
                DO model = new DO();
                _copyDO.Invoke(item, model);
                DIList.Add(model);
            }
            foreach (DO item in DIList)
            {
                var model = IOP.DODataSet.FirstOrDefault(x => x.Index == item.Index);
                if (model != null)
                {
                    IOP.DODataSet.Remove(model);
                }
            }
            for (int i = 0; i < IOP.DIDataSet.Count; i++)
            {
                IOP.DODataSet[i].Index = i + 1;
            }
            DODataSet.Clear();
            DODataSet = new ObservableCollection<DO>(IOP.DODataSet);
            IOP.Save();
        });
    }
}
